<template>
  <el-container class="server-container">
    <el-aside width="250px">
      <!-- openvidu选择框 -->
      <el-row>
        <el-select v-model="openviduSelect" @change="serversData" placeholder="请选择OpenVidu服务" class="aside-select">
          <el-option
            v-for="item in openviduInfo"
            :key="item.hostname"
            :value="item.hostname">
          </el-option>
        </el-select>
      </el-row>
      <!-- 会话搜索框 -->
      <el-row>
        <el-input v-model="searchData" @change="searchServer" suffix-icon="el-icon-search" placeholder="请输入会议名称" class="aside-search"/>
        <el-button icon="el-icon-refresh" @click="serversData" plain/>
      </el-row>
      <!--  会话列表    -->
      <div v-for="item in sessionShowList" :key="item.sessionId">
        <div @click="selectSession(item)">
          <el-card class="box-card" v-bind:class="{ 'choose-card' : (chooseSession.sessionId === item.sessionId)}">
            <div slot="header">
              <span>{{item.sessionId}}</span>
              <el-popconfirm
                confirmButtonText='确认'
                cancelButtonText='取消'
                icon="el-icon-info"
                iconColor="red"
                title="确定强制断开会话吗？"
                @onConfirm="closeSession(item)"
                v-loading.fullscreen.lock="fullscreenLoading"
              >
                <el-button slot="reference" size="mini" type="danger" style="float: right;padding: 5px" >关闭</el-button>
              </el-popconfirm>
              <div style="font-size: smaller">{{item.createdAt}}</div>
            </div>
            <div class="box-card-body">
              <el-tooltip class="item" effect="dark" content="连接数" placement="bottom">
                <el-badge :value="item.connectCount" class="badge-item" >
                  <el-button size="mini" icon="el-icon-s-custom" circle/>
                </el-badge>
              </el-tooltip>
              <el-tooltip class="item" effect="dark" content="发布数" placement="bottom">
                <el-badge :value="item.pubCount" class="badge-item">
                  <el-button size="mini" icon="el-icon-top-right" circle/>
                </el-badge>
              </el-tooltip>
              <el-tooltip class="item" effect="dark" content="订阅数" placement="bottom">
                <el-badge :value="item.subCount" class="badge-item" type="primary">
                  <el-button size="mini" icon="el-icon-bottom-left" circle/>
                </el-badge>
              </el-tooltip>
              <el-popconfirm
                confirmButtonText='确认'
                cancelButtonText='取消'
                icon="el-icon-info"
                iconColor="red"
                title="确定开启录制吗？"
                @onConfirm="startRecord(item)"
              >
                <el-button slot="reference" size="mini" v-if="!item.recording" type="success" icon="el-icon-video-camera-solid" circle/>
              </el-popconfirm>
              <el-popconfirm
                confirmButtonText='确认'
                cancelButtonText='取消'
                icon="el-icon-info"
                iconColor="red"
                title="确定停止录制吗？"
                @onConfirm="stopRecord(item)"
              >
                <el-button slot="reference" size="mini" v-if="item.recording" icon="el-icon-close" circle/>
              </el-popconfirm>
            </div>
          </el-card>
        </div>
      </div>
    </el-aside>
    <el-container>
      <el-main>
        <el-row>
          <el-divider content-position="left">当前会议ID: {{chooseSession.sessionId}}</el-divider>
        </el-row>
        <el-row>
          <el-table
            :data="chooseSession.connections"
            stripe
            border
            style="width: 100%"
          >
            <!--    表单扩展-连接详情        -->
            <el-table-column type="expand" size="mini">
              <template slot-scope="{row}">
                <el-form v-for="pub in row.publishers" :key="pub.streamIde" label-position="left" inline class="con-table-expand">
                  <el-divider content-position="left">发布流ID: {{ pub.streamId }} ，创建时间：{{ pub.createdAt }} &#12288;
                    <el-popconfirm
                      confirmButtonText='确认'
                      cancelButtonText='取消'
                      icon="el-icon-info"
                      iconColor="red"
                      title="确定强制取消发布吗？"
                      @onConfirm="forceUnpublish(pub)"
                      v-loading.fullscreen.lock="fullscreenLoading"
                    >
                      <el-button slot="reference" size="mini" type="danger" style="float: right;padding: 5px">取消发布</el-button>
                    </el-popconfirm>
                  </el-divider>
                  <el-form-item label="类 型" style="width: 150px;">
                    <span>{{ pub.typeOfVideo }}</span>
                  </el-form-item>
                  <!--                    <el-form-item label="hasVideo">-->
                  <!--                      <span>{{ pub.hasVideo }}</span>-->
                  <!--                    </el-form-item>-->
                  <!--                    <el-form-item label="hasAudio">-->
                  <!--                      <span>{{ pub.hasAudio }}</span>-->
                  <!--                    </el-form-item>-->
                  <el-form-item label="视 频" style="width: 100px;">
                    <span>
                      <i class="el-icon-video-camera" v-if="pub.videoActive"/>
                      <i class="el-icon-close" v-if="!pub.videoActive"/>
                    </span>
                  </el-form-item>
                  <el-form-item label="音 频" style="width: 100px;">
                    <span>
                      <i class="el-icon-microphone" v-if="pub.audioActive"/>
                      <i class="el-icon-turn-off-microphone" v-if="!pub.audioActive"/>
                    </span>
                  </el-form-item>
                  <el-form-item label="帧 率" style="width: 100px;">
                    <span>{{ pub.frameRate }}</span>
                  </el-form-item>
                  <el-form-item label="分辨率" style="width: 250px;">
                    <span>{{ pub.videoDimensions }}</span>
                  </el-form-item>
                </el-form>
                <el-divider v-for="sub in row.subscribers" :key="sub" content-position="left">订阅流ID {{ sub }}</el-divider>
              </template>
            </el-table-column>
            <!--     表单列表       -->
            <el-table-column
              label="操作"
              width="80"
              align="center"
            >
              <template slot-scope="{row}">
                <el-popconfirm
                  confirmButtonText='确认'
                  cancelButtonText='取消'
                  icon="el-icon-info"
                  iconColor="red"
                  title="确定强制断开连接吗？"
                  @onConfirm="disConnection(row)"
                  v-loading.fullscreen.lock="fullscreenLoading"
                >
                  <el-button slot="reference" size="mini" type="danger" style="float: right;padding: 5px">断开连接</el-button>
                </el-popconfirm>
              </template>
            </el-table-column>
            <el-table-column
              prop="connectionId"
              label="连接ID"
              width="150"
              align="center"
            />
            <el-table-column
              prop="createdAt"
              label="创建时间"
              width="160"
              align="center"
            />
            <el-table-column
              prop="role"
              label="角色"
              width="100"
              align="center"
            />
            <el-table-column
              prop="clientData"
              label="客户端信息"
              width="150"
              align="center"
              resizable
              show-overflow-tooltip
            />
            <el-table-column
              prop="serverData"
              label="服务端信息"
              width="150"
              align="center"
              resizable
              show-overflow-tooltip
            />
            <el-table-column
              prop="platform"
              label="平台信息"
              width="150"
              align="center"
              show-overflow-tooltip
            />
            <el-table-column
              prop="publishers.length"
              label="发布数"
              width="80"
              align="center"
            />

            <el-table-column
              prop="subscribers.length"
              label="订阅数"
              width="80"
              align="center"
            />
          </el-table>
        </el-row>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
import { ovinfoAll } from '@/api/openviduInfo'
import { openviduDetail, sessionClose, sessionForceDisconnect, sessionForceUnpublish } from '@/api/server'
import { startRecording, stopRecording } from '@/api/record'

export default {

  data() {
    return {
      preSelect:null,
      openviduSelect: null,
      openviduInfo: [],
      sessionSourceList: [],
      searchData: null,
      sessionShowList:[],
      chooseSession:{},
      fullscreenLoading:false
    }
  },
  mounted: function () {
    this.preSelect = this.$route.params.hostname;
    console.log(this.preSelect);
    this.selectList();
  },
  methods: {
    selectList() {
      return new Promise((resolve, reject) => {
        ovinfoAll({}).then(response => {
          const { success, data, message } = response
          if (success) {
            this.openviduInfo = data;
            for (let item of this.openviduInfo) {
              console.log(item);
              console.log(item.hostname);
              if (this.preSelect !== null && this.preSelect === item.hostname) {
                this.openviduSelect = this.preSelect;
                this.serversData();
                this.preSelect = null;
                break;
              }
            }
          }
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },
    serversData() {
      if (this.openviduSelect === undefined || this.openviduSelect === null) {
        return
      }
      return new Promise((resolve, reject) => {
        openviduDetail({
          hostname: this.openviduSelect
        }).then(response => {
          const { success, data, message } = response
          if (success) {
            this.sessionSourceList = data.activeSessions;
            this.searchServer()
          }
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },
    searchServer() {
      console.log("this.searchData:"+this.searchData)
      if (this.searchData === undefined || this.searchData === null) {
        this.sessionShowList = this.sessionSourceList;
        return
      }
      const search = this.searchData;
      this.sessionShowList = this.sessionSourceList.filter(function (item, index) {
        return item.sessionId.indexOf(search) !== -1;
      });
    },
    selectSession(session) {
      // 表单刷新
      this.chooseSession = session
    },
    loading() {
      this.fullscreenLoading = true;
      setTimeout(() => {
        this.fullscreenLoading = false;
      }, 1000);
    },
    closeSession(session) {
      // 断开会话
      console.log("断开会话");
      return new Promise((resolve, reject) => {
        sessionClose({
          hostname: this.openviduSelect,
          sessionId: session.sessionId,
        }).then(response => {
          const { success, data, message } = response;
          if (success) {
            this.$message('断开会话'+success);
            if (session.sessionId === this.chooseSession.sessionId) {
              this.selectSession({});
            }
            this.loading();
            this.serversData();
          }
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },
    disConnection(connection) {
      // 断开连接
      console.log("断开连接");
      return new Promise((resolve, reject) => {
        sessionForceDisconnect({
          hostname: this.openviduSelect,
          sessionId: this.chooseSession.sessionId,
          connectionId: connection.connectionId
        }).then(response => {
          const { success, data, message } = response;
          if (success) {
            this.$message('断开连接'+success);
            this.loading();
            this.serversData();
            this.selectSession(this.chooseSession);
          }
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },
    forceUnpublish(stream) {
      // 取消发布
      console.log("取消发布");
      return new Promise((resolve, reject) => {
        sessionForceUnpublish({
          hostname: this.openviduSelect,
          sessionId: this.chooseSession.sessionId,
          publishStreamId: stream.streamId
        }).then(response => {
          const { success, data, message } = response;
          if (success) {
            this.$message('取消发布'+success);
            this.loading();
            this.serversData();
            this.selectSession(this.chooseSession);
          }
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },
    startRecord(item) {
      return new Promise((resolve, reject) => {
        startRecording({
          hostname: this.openviduSelect,
          sessionId: item.sessionId
        }).then(response => {
          const { success, data, message } = response;
          if (success) {
            this.$message(message);
            this.loading();
            this.serversData();
            this.selectSession(this.chooseSession);
          }
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },
    stopRecord(item) {
      return new Promise((resolve, reject) => {
        stopRecording({
          hostname: this.openviduSelect,
          sessionId: item.sessionId
        }).then(response => {
          const { success, data, message } = response;
          if (success) {
            this.$message(message);
            this.loading();
            this.serversData();
            this.selectSession(this.chooseSession);
          }
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    }
  }
}
</script>

<style type="scss">
  .el-aside {
    background-color: #D3DCE6;
    color: #333;
  }

  .el-main {
    background-color: #D3DCE6;
    color: #333;
    padding: 5px 5px;
  }

  .server-container {
    background-color: #D3DCE6;
    height: 1250px;
  }

  .badge-item {
     margin-right: 18px;
  }

  .box-card {
    margin-left: 5px;
    margin-top: 5px;
    width: 240px;
  }

  .aside-select {
    margin-left: 5px;
    margin-top: 5px;
    width: 240px;
  }

  .aside-refresh {
    width: 40px;
  }

  .aside-search {
    margin-left: 5px;
    margin-top: 5px;
    width: 180px;
  }

  .con-table-expand {
    font-size: 0;
  }
  .con-table-expand label {
    width: 60px;
    color: #99a9bf;
  }
  .con-table-expand .el-form-item {
    margin-right: 0;
    margin-bottom: 0;
    width: 15%;
  }

  .choose-card {
    background-color: goldenrod;
  }
</style>

